// pages/list/list.js

const fetch = require('../../utils/promise.js');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    categories:{},
    shops : [],
    pageIndex : 0,
    pageSize:10,
    hasMore:true
  },
  loadMore:function(){
    if(!this.data.hasMore) return;
    let { pageIndex, pageSize} = this.data;
    const params = { _page: ++pageIndex, _limit: pageSize };
    
    console.log(params._page)

    // console.log(pageIndex)
    console.log(this.data)
    return fetch(`categories/${this.data.categories.id}/shops`, params).then(res=>{
      let totalCount = parseInt(res.header['X-Total-Count']);
      let counts = Math.ceil(totalCount / pageSize);
      let hasMore = pageIndex * pageSize < totalCount;

      let shops = this.data.shops.concat(res.data);
      this.setData({ shops: shops, pageIndex: params._page, hasMore})
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options)
    fetch(`categories/${options.cat}`).then(res=>{
      // console.log(res.data)
      this.setData({ categories:res.data });
      wx.setNavigationBarTitle({
        title: res.data.name
      })
    //   return fetch(`categories/${this.data.categories.id}/shops`,{ _page:1,_limit:10 });
    // })
    // .then(res=>{
    //   this.setData({ shops: res.data });
    this.loadMore()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    if(this.data.categories.name){
      wx.setNavigationBarTitle({
        title: this.data.categories.name,
      });
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({ shops:[], pageIndex:0 });
    this.loadMore().then(()=>{
      wx.stopPullDownRefresh()
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("到底了，别拉了");
    this.loadMore()
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})